/**
 * 
 */
package com.nextive.interview.dao.sqlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Sqlite helper class. It is in charge of opening/closing database connections 
 * as well as creating/upgrading databases.
 * Any Dao accessing Sqlite database should extend this class.
 * @author Cesar Roman
 *
 */
public abstract class SqLiteHelperDao {
	
	/**
	 * Data base helper.
	 */
	private SqLiteDBHelper dbHelper;
	
	/**
	 * Application's context.
	 */
	private Context context;
	
	/**
	 * Constructor. It receives application's context.
	 * @param context application's context
	 */
	public SqLiteHelperDao(Context context) {
		
		this.context = context;
		
	}
	
	/**
	 * It is in charge of closing the database via data base helper.
	 */
	protected void close() {
	
		dbHelper.close();
		
	}
	
	/**
	 * It is in charge of obtaining a reference to the database. 
	 * If database connection is closed, then it opens it and returns it.
	 * @return the data base connection
	 */
	protected SQLiteDatabase getDatabase() {
		
		if (dbHelper == null)
			dbHelper = new SqLiteDBHelper(context);
		
		return dbHelper.getWritableDatabase();
		
	}
	
	/**
	 * It fetches the database name.
	 * @return database name
	 */
	protected abstract String getDBName();
	
	/**
	 * It fetches the database version.
	 * @return database version
	 */
	protected abstract int getDBVersion();
	
	/**
	 * It fetches the database create statement.
	 * @return database create statement
	 */
	protected abstract String getDBCreateStatement();
	
	/**
	 * It fetches the database drop statement.
	 * @return database drop statement
	 */
	protected abstract String getDBDropStatement();
	
	/**
	 * Database helper class. It is used by <code>SqLiteHelperDao</code> 
	 * class in order to manipulate database connections.
	 * @author Cesar Roman
	 *
	 */
	private class SqLiteDBHelper extends SQLiteOpenHelper {

    	/**
    	 * Default constructor. It receives application's context
    	 * @param context the application's context
    	 */
		SqLiteDBHelper(Context context) {
    		
            super(context, getDBName(), null, getDBVersion());
            
        }

        /**
         * Implementation of onCreate method. It is in charge of creating 
         * the database from scratch.
         * @param db the database
         */
		@Override
        public void onCreate(SQLiteDatabase db) {

            db.execSQL(getDBCreateStatement());
            
        }

        /**
         * Implementation of onUpgrade method. It is in charge of upgrading
         * the database.
         * @param db the database
         * @param oldVersion
         * @param newVersion
         */
		@Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        	
            db.execSQL(getDBDropStatement());
            onCreate(db);
            
        }
    }

}